#include "../../HeadFiles/StandardHeads.h"
#include "../../HeadFiles/ChainsH/ChainOpt.h"


void backupAll()
{
    //RecordData.txt
    FILE * recordFile = fopen("../Data/RecordChainData/RecordData.txt", "r");
    FILE * recordFile_backup = fopen("../Data/RecordChainData/RecordData_backup.txt", "w");
    char ch = fgetc(recordFile);
    while(ch != EOF)
    {
        fputc(ch, recordFile_backup);
        ch = fgetc(recordFile);
    }
    fclose(recordFile);     fclose(recordFile_backup);
    recordFile = recordFile_backup = NULL;

    //ClientData.txt
    FILE * clientFile = fopen("../Data/ClientChainData/ClientData.txt", "r");
    FILE * clientFile_backup = fopen("../Data/ClientChainData/ClientData_backup.txt", "w");
    ch = fgetc(clientFile);
    while(ch != EOF)
    {
        fputc(ch, clientFile_backup);
        ch = fgetc(clientFile);
    }
    fclose(clientFile);     fclose(clientFile_backup);
    clientFile = clientFile_backup = NULL;


    //CommodityData.txt
    FILE * commodityFile = fopen("../Data/CommodityChainData/CommodityData.txt", "r");
    FILE * commodityFile_backup = fopen("../Data/CommodityChainData/CommodityData_backup.txt", "w");
    ch = fgetc(commodityFile);
    while(ch != EOF)
    {
        fputc(ch, commodityFile_backup);
        ch = fgetc(commodityFile);
    }
    fclose(commodityFile);     fclose(commodityFile_backup);
    commodityFile = commodityFile_backup = NULL;

    //AdminData.txt
    FILE * adminDataFile = fopen("../Data/LoginData/AdminData.txt", "r");
    FILE * adminDataFile_backup = fopen("../Data/LoginData/AdminData_backup.txt", "w");
    ch = fgetc(adminDataFile);
    while(ch != EOF)
    {
        fputc(ch, adminDataFile_backup);
        ch = fgetc(adminDataFile);
    }
    fclose(adminDataFile);     fclose(adminDataFile_backup);
    adminDataFile = adminDataFile_backup = NULL;

    //UserData.txt
    FILE * userDataFile = fopen("../Data/LoginData/UserData.txt", "r");
    FILE * userDataFile_backup = fopen("../Data/LoginData/UserData_backup.txt", "w");
    ch = fgetc(userDataFile);
    while(ch != EOF)
    {
        fputc(ch, userDataFile_backup);
        ch = fgetc(userDataFile);
    }
    fclose(userDataFile);     fclose(userDataFile_backup);
    userDataFile = userDataFile_backup = NULL;

    //LockedUser.txt
    FILE * lockedUserFile = fopen("../Data/LoginData/LockedUser.txt", "r");
    FILE * lockedUserFile_backup = fopen("../Data/LoginData/LockedUser_backup.txt", "w");
    ch = fgetc(lockedUserFile);
    while(ch != EOF)
    {
        fputc(ch, lockedUserFile_backup);
        ch = fgetc(lockedUserFile);
    }
    fclose(lockedUserFile);     fclose(lockedUserFile_backup);
    lockedUserFile = lockedUserFile_backup = NULL;

    //WrongPwdTimes.txt
    FILE * wrongPwdTimesFile = fopen("../Data/LoginData/WrongPwdTimes.txt", "r");
    FILE * wrongPwdTimesFile_backup = fopen("../Data/LoginData/WrongPwdTimes_backup.txt", "w");
    ch = fgetc(wrongPwdTimesFile);
    while(ch != EOF)
    {
        fputc(ch, wrongPwdTimesFile_backup);
        ch = fgetc(wrongPwdTimesFile);
    }
    fclose(wrongPwdTimesFile);     fclose(wrongPwdTimesFile_backup);
    wrongPwdTimesFile = wrongPwdTimesFile_backup = NULL;
}

// 从main.c引入链表头指针
extern struct RecordNode * recordHead;
extern struct ClientNode * clientHead;
extern struct CommodityNode * commodityHead;
void releaseAll()
{
    // RecordChain
    struct RecordNode * p = recordHead->nextRecord;
    while(p != NULL)
    {
        recordHead = p;
        p = p->nextRecord;
        free(recordHead);
    }

    // ClientChain
    struct ClientNode * q = clientHead->nextClient;
    while(p != NULL)
    {
        clientHead = q;
        q = q->nextClient;
        free(clientHead);
    }

    //CommodityChain
    struct CommodityNode * p1 = commodityHead->nextCommodity;
    while(p1 != NULL)
    {
        commodityHead = p1;
        p1 = p1->nextCommodity;
        free(commodityHead);
    }
}

struct ClientNode * getClientPtrFromName(char yourName[])
{
    struct ClientNode * searchPtr = clientHead;
    struct ClientNode * res = NULL;
    while (searchPtr != NULL)
    {
        if (strcmp(searchPtr->clientName, yourName) == 0)
        {
            res = searchPtr;
            return res;
        }
        searchPtr = searchPtr->nextClient;
    }
    return res;
}